<template>
  <div>
     <!-- 面包屑 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>权限管理</el-breadcrumb-item>
      <el-breadcrumb-item>权限列表</el-breadcrumb-item>
    </el-breadcrumb>
     <!-- 卡片 -->
    <el-card>
      <el-alert
        title="添加商品信息"
        type="info"
        center
        show-icon
        :closable="false">
      </el-alert>
      <!-- 进度条 -->
      <el-steps :active="active - 0" finish-status="success" align-center>
        <el-step title="基本信息" ></el-step>
        <el-step title="商品参数" ></el-step>
        <el-step title="商品属性"></el-step>
        <el-step title="商品图片"></el-step>
        <el-step title="商品内容"></el-step>
        <el-step title="完成"></el-step>
      </el-steps>
      <el-form ref="goodsFormRef" :model="goodsForm" :rules="goodsRules">
        <el-tabs tab-position="left"  @tab-click="goRun"  v-model="active" :before-leave="tagTab">
        <!-- 表单 -->
          <el-tab-pane label="基本信息" name="0">
              <el-form-item label="商品名称" prop="goods_name">
                <el-input v-model="goodsForm.goods_name"></el-input>
              </el-form-item>
              <el-form-item label="商品价格" prop="goods_price">
                <el-input v-model="goodsForm.goods_price" type="number" ></el-input>
              </el-form-item>
              <el-form-item label="商品重量" prop="goods_weight">
                <el-input v-model="goodsForm.goods_weight"></el-input>
              </el-form-item>
              <el-form-item label="商品数量" prop="goods_number">
                <el-input v-model="goodsForm.goods_number"></el-input>
              </el-form-item>
              <el-form-item label="商品分类" prop="goods_number">
                <el-cascader
                v-model="catId"
                :options="ParentRolesList"
                :props="{ expandTrigger: 'hover', value: 'cat_id', label: 'cat_name', children: 'children'}"
                @change="ParentRolesChange" clearable>
                </el-cascader>
              </el-form-item>
          </el-tab-pane>
          <el-tab-pane label="商品参数" name="1">配置管理</el-tab-pane>
          <el-tab-pane label="商品属性" name="2">角色管理</el-tab-pane>
          <el-tab-pane label="商品图片" name="3"></el-tab-pane>
          <el-tab-pane label="商品内容" name="4"></el-tab-pane>
        </el-tabs>
      </el-form>
      </el-card>
  </div>
</template>

<script>
export default {
  data () {
    return {
      active: '0', // 绑定进度条和左侧选项卡的数值。进度条需要数字，左侧需要字符串
      goodsForm: {},
      goodsRules: { // 表单验证规则
        goods_name: [
          { required: true, message: '请输入商品名称', trigger: 'blur' },
          { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
        goods_price: [
          { required: true, message: '请输入商品价格', trigger: 'blur' },
          { type: 'number', required: true, message: '请输入数字', trigger: 'blur' }
        ],
        goods_weight: [
          { required: true, message: '请输入商品名称', trigger: 'blur' },
          { type: 'number', required: true, message: '请输入数字', trigger: 'blur' }
        ],
        goods_number: [
          { required: true, message: '请输入商品名称', trigger: 'blur' },
          { type: 'number', required: true, message: '请输入数字', trigger: 'blur' }
        ]
        // ,
        // goods_cat: [
        //   { required: true, message: '请填写', trigger: 'change' }
        // ]
      },
      catId: [],
      ParentRolesList: []
    }
  },
  created () {
    this.getParentRolesList()
  },
  methods: {
    goRun () {},
    ParentRolesChange () { // 下拉框发生改变
      if (this.catId.length === 0) return
      if (this.catId.length !== 3) {
        this.catId = []
        return this.$message.error('只允许选择三级分类')
      }
      this.goodsForm.goods_cat = this.catId.join(',') // 可口要求字符串，所以需要转化
    },
    async getParentRolesList () {
      const { data: res } = await this.$http.get('categories')
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.ParentRolesList = res.data
    },
    tagTab () { // 阻止标签切换
      if (this.catId.length !== 3) {
        this.$message.error('必须选择商品分类')
        return false
      }
    }
  }
}
</script>

<style lang="less" scoped>
.el-steps {
  margin-top: 15px;
  margin-bottom: 22px;
  font-size: 12px!important;
}
</style>
